/*
 * twist.cpp
 * 
 * Copyright 2012 Maximilian Eisbach <max@itp.tu-berlin.de>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */
#include <mylibs/myexception.hpp>
#include <mylibs/CardiacMesh.hpp>
#include <mylibs/myinifiles.hpp>
#include <mylibs/cmdline.hpp>
#include <iostream>
#include <fstream>
#include <sys/stat.h> 	// mkdir

using namespace std;

bool verbose = false;

void set_regions(double endo_mcell, double mcell_epi, CardiacMesh &mesh) {
	
	/*if (endo_mcell > mcell_epi || endo_mcell == mcell_epi) throw myexception("Transition Endo to M-Cell must be smaller than M-Cell to Epi and cannot be equal");
	if (endo_mcell >= 1 || endo_mcell <=0) throw myexception("Position must be element of [0,1]");
	if (mcell_epi >= 1 || mcell_epi <=0) throw myexception("Position must be element of [0,1]");*/

//	Face *faces = &(mesh.f[0]);
	
	for (size_t i = 0; i < mesh.intra_elements(); i++){

		
		const size_t &idx = mesh.elem_i2e(i);
		//ENDO
		if (mesh.transmural_position(i) > endo_mcell){
			mesh.f[idx].attribute = 23;
		}
		//EPI
		else if (mesh.transmural_position(i) < mcell_epi){
			mesh.f[idx].attribute = 21;
		}
		//M-Cell
		else {
			mesh.f[idx].attribute = 22;
		}
	}
	return;
}

void set_fiber_twist(double deg) {}

//done later
void set_fiber_twist(double deg, bool function) {}

int main(int argc, char **argv){
	myIniFiles ini(argc, argv);
	
	ini.set_info("Compute the fiber and celltype structure for a given mesh.");
	ini.register_param("mesh", "m", "Mesh basename without extension.");
	ini.register_param("T1", "1", "Threshold Endo <> M-cell ( Endo = 1, EPI=0. ) ");
	ini.register_param("T2", "2", "Threshold M-cell <> Epi, ( Endo = 1, EPI=0. ) " );
	
	ini.check(2);
	
	mystring meshfile;
	mystring output 	= mystring();
	double endo_mcell	= 0.3;
	double mcell_epi 	= 0.1;

	
	meshfile = ini.read("mesh", mystring());
	endo_mcell = ini.read("T1", endo_mcell);
	mcell_epi  = ini.read("T2", mcell_epi);
		
	if (meshfile.empty()) 
		ini.print_help("mesh");

	//load the standard input mesh
	CardiacMesh mesh(meshfile, true);
	// Get i2e sequence structure
	mesh.read_lon();
	// Get transmural positions
	mesh.read_transmural_pos();

	set_regions(endo_mcell, mcell_epi, mesh);
	
	output = "test";
	mesh.save_mesh(output.c_str());
	
	return 0;
}




